/*
 * @Author: wuqingchun
 * @Date: 2024-04-20 14:23:37
 * @Description: 静态数组实现
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-04-22 08:40:04
 */
#ifndef __MY_ARRAY_H__
#define __MY_ARRAY_H__

#include <cstddef>
#include <stdexcept>

template <typename T, size_t MAX_SIZE>
class MyArray
{
public:
    // 迭代器类实现
    class iterator
    {
    private:
        T* m_pData;
        size_t m_ulCurPos = 0;
    public:
        iterator(T* pData, size_t ulPos):
            m_pData(pData),
            m_ulCurPos(ulPos)
        {

        }

        // 前置递增
        iterator& operator++()
        {
            m_ulCurPos++;
            return *this;
        }

        // 后置递增
        iterator operator++(int)
        {
            iterator temp(m_pData, m_ulCurPos);
            m_ulCurPos++;
            return temp;
        }

        T& operator* ()
        {
            return m_pData[m_ulCurPos];
        }

        bool operator==(const iterator& refOther)
        {
            if (&refOther == this ||
                (refOther.m_pData == this->m_pData && refOther.m_ulCurPos == this->m_ulCurPos))
            {
                return true;
            }

            return false;
        }

        bool operator!=(const iterator& refOther)
        {
            if (refOther.m_ulCurPos != this->m_ulCurPos)
            {
                return true;
            }

            return false;
        }
    };

public:

    // 用户自定义的类，它们可以定义构造函数来接受 std::initializer_list 参数，从而支持使用 = {} 的语法进行赋值。
    MyArray(std::initializer_list<T> initList)
    {
        auto it = initList.begin();
        for (size_t ulPos = 0; ulPos < MAX_SIZE, it < initList.end(); ulPos++, it++)
        {
            m_atData[ulPos] = *it;
        }
    }

    /**
     * @brief Construct a new My Array object
     * 拷贝构造
     * @param refArray 
     */
    MyArray(const MyArray<T, MAX_SIZE>& refArray)
    {
        for(size_t ulPos = 0; ulPos < MAX_SIZE; ulPos++)
        {
            m_atData[ulPos] = refArray.m_atData[ulPos];
        }
    }

    iterator begin()
    {
        return iterator(m_atData, 0);
    }

    iterator end()
    {
        return iterator(m_atData, MAX_SIZE);
    }
    
    T& at(size_t ulPos)
    {
        if (ulPos < MAX_SIZE)
        {
            return m_atData[ulPos];
        }

        throw std::out_of_range("bad array index");
    }

    T& operator[](size_t ulPos)
    {
        return m_atData[ulPos];
    }

private:
    T m_atData[MAX_SIZE];
};


#endif //__MY_ARRAY_H__